package com.kaifamiao.wendao.controller;

import com.alibaba.fastjson2.JSONObject;
import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.entity.Explain;
import com.kaifamiao.wendao.entity.Level;
import com.kaifamiao.wendao.entity.Topic;
import com.kaifamiao.wendao.service.CustomerService;
import com.kaifamiao.wendao.service.ExplainService;
import com.kaifamiao.wendao.service.LevelService;
import com.kaifamiao.wendao.service.TopicService;
import com.kaifamiao.wendao.utils.Constants;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.tinylog.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet("/customer/*")
public class CustomerServlet extends HttpServlet {

    private CustomerService service = new CustomerService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Logger.info("注册调用");

        PrintWriter out = resp.getWriter();
        Map<String,Object> map = new HashMap<>();

        Customer c = new Customer();
        Logger.info(c);
        String username=req.getParameter("username");
        c.setUsername(username);
        String password = req.getParameter("password");
        String confirm = req.getParameter("confirm");
        if (!org.apache.commons.lang3.StringUtils.equals(password,confirm)){
            map.put("success", false);
            map.put("message", "两次密码不一致");
            out.println( JSONObject.toJSONString( map ) );
            return;
        }
        c.setPassword(password);
        String nickname = req.getParameter("nickname");

        c.setNickname(nickname);



        try{
            boolean res = service.save(c);

            if (res){
                map.put("success", true);
                map.put("message", "注册成功");
            }else{
                map.put( "success", false );
                map.put( "message", "注册失败" );
            }
        }catch (Exception e){
            map.put( "success", false );
            map.put( "message", "保存失败" );
            map.put( "cause", e.getMessage() );
        }
        out.println( JSONObject.toJSONString( map ) );

    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        Map<String,Object> map = new HashMap<>();

        String sid = req.getParameter( "id" );
        if( com.mysql.cj.util.StringUtils.isNullOrEmpty(sid) || StringUtils.isEmptyOrWhitespaceOnly(sid) ){
            map.put("success", false);
            map.put("message", "被删除学生编号不能为空");
            out.println( JSONObject.toJSONString( map ) );
            return;
        }

        Long id = null;
        try {
            id = Long.valueOf(sid);
        } catch (Exception e ) {
            Logger.error(e, "学生编号必须是整数形式");
            map.put("success", false);
            map.put("message", "学生编号必须是整数形式");
            out.println( JSONObject.toJSONString( map ) );
            return;
        }
        try {
            boolean res = service.remove(id);
            if(res) {
                map.put("success", true);
                map.put("message", "删除成功");
            } else {
                map.put("success", false);
                map.put("message", "删除失败");
            }
        } catch (Exception e) {
            Logger.error(e, "学生信息删除失败");
            map.put("success", false);
            map.put("message", "删除失败");
            map.put("cause", e.getMessage());
        }

        out.println( JSONObject.toJSONString( map ) );
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应头中 content-type 字段 (即设置MIME类型)
        resp.setContentType( "text/plain;charset=UTF-8");
        // 请务必记得调用重写的service方法 (因为父类中的service会根据HTTP动作分发请求)
        super.service(req, resp);
    }
}
